package com.lzx.reggie.common

import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler
import org.apache.ibatis.reflection.MetaObject
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.time.LocalDateTime

@Component
class MyMetaObjectHandler : MetaObjectHandler {

    companion object {
        private val log = LoggerFactory.getLogger(MyMetaObjectHandler::class.java)
    }

    override fun insertFill(metaObject: MetaObject) {
        log.info("公共字段自动填充[insert]...")
        log.info(metaObject.toString())

        metaObject.apply {
            setValue("createTime", LocalDateTime.now())
            setValue("updateTime", LocalDateTime.now())
            setValue("createUser", BaseContext.currentId)
            setValue("updateUser", BaseContext.currentId)
        }
    }

    override fun updateFill(metaObject: MetaObject) {
        log.info("公共字段自动填充[update]...")
        log.info(metaObject.toString())

        Thread.currentThread().run {
            log.info("线程id为：$id")
        }

        metaObject.apply {
            setValue("updateTime", LocalDateTime.now())
            setValue("updateUser", BaseContext.currentId)
        }
    }
}